package com.biz.primus.common.support.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * PrimusLocalDateTimeDeserializer
 *
 * 转换Java8 LocalDateTime 到 yyyy-MM-dd HH:mm:ss格式的时间
 *
 * @author david-liu
 */
public class PrimusLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    private static final DateTimeFormatter PRIMUS_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return LocalDateTime.from(PRIMUS_DATETIME_FORMATTER.parse(p.getText().trim()));
    }
}
